<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
      @submit.native.prevent="() => handleQuery()"
    >
      <el-form-item :label="$t('plan_name')" prop="schemeName">
        <el-input
          v-model="queryParams.schemeName"
          :placeholder="$t('please_enter_plan_name')"
          :show-word-limit="true"
          clearable
        />
      </el-form-item>
      <!-- <el-form-item label="负责人" prop="schemeName">
        <el-input
          v-model="queryParams.schemeName"
          placeholder="请输入负责人"
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{$t('search')}}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >{{$t('reset')}}</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
      <el-radio-group v-model="queryParams.schemeType" @change="getList">
        <el-radio-button
          :label="item.value"
          v-for="item in dict.type.contribution_plan_type"
          :key="item.value"
          >{{ item.label }}</el-radio-button
        >
      </el-radio-group>
      <div>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['employer:contributionPlan:export']"
            >{{$t('export')}}</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['employer:contributionPlan:add']"
            >{{$t('add_new_plan')}}</el-button
          >
        </el-col>
      </div>
    </el-row>

    <el-table
      v-loading="loading"
      :data="schemeList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t('plan_name')"
        align="center"
        prop="schemeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('plan_type')"
        align="center"
        prop="schemeType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.contribution_plan_type"
            :value="scope.row.schemeType"
          />
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('plan_code')"
        align="center"
        prop="schemeCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('plan_number')"
        align="center"
        prop="schemeNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('employer_name')"
        align="center"
        prop="employerName"
        :show-overflow-tooltip="true"
      />

      <template v-if="queryParams.schemeType == 1">
        <el-table-column
          :label="$t('employer_participation_number')"
          align="center"
          prop="employerParticipationNumber"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('contact_person_name')"
          align="center"
          prop="contactName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('contact_number')"
          align="center"
          prop="contactPhone"
          :show-overflow-tooltip="true"
        />
      </template>

      <template v-if="queryParams.schemeType == 0">
        <el-table-column
          :label="$t('offset_with_employer_mpf')"
          align="center"
          prop="hedgeWithEmployerMpf"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('offset_with_employee_mpf')"
          align="center"
          prop="hedgeWithEmployeeMpf"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('employer_contribution_ceiling')"
          align="center"
          prop="employerContributionCap"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('employee_contribution_ceiling')"
          align="center"
          prop="employeeContributionCap"
          :show-overflow-tooltip="true"
        />
      </template>

      <!-- <el-table-column
        label="雇主负责人"
        align="center"
        prop=""
        :show-overflow-tooltip="true"
      /> -->
      <!-- <el-table-column
        label="状态"
        align="center"
        prop="state"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.common_status"
            :value="scope.row.state"
            :style="scope.row.state == 0 ? 'color:red' : ''"
          />
        </template>
      </el-table-column> -->
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['employer:contributionPlan:edit']"
            >{{$t('modify')}}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['employer:contributionPlan:delete']"
            @click="handleDelete(scope.row)"
            >{{ $t('delete') }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改供款计划对话框 -->
    <addOrUp ref="addOrUpdata" @handleSuccess="handleSuccess"></addOrUp>
  </div>
</template>

<script>
import { listScheme, delScheme } from "@/api/custom/contributionPlan";
import addOrUp from "./addOrUpdata.vue";
export default {
  name: "Scheme",
  dicts: [
    "contribution_plan_type",
    "common_status",
    "contribution_voluntarily",
  ],
  components: {
    addOrUp,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供款计划表格数据
      schemeList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schemeName: null,
        schemeType: 1,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSuccess() {
      this.getList();
    },
    /** 查询供款计划列表 */
    getList() {
      this.loading = true;
      listScheme(this.queryParams).then((response) => {
        this.schemeList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addOrUpdata.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.addOrUpdata.handleUpdate({ ...row });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t('delete_data'))
        .then(function () {
          return delScheme(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('operation_successful'));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/employer/scheme/export",
        {
          ...this.queryParams,
        },
        `scheme_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
